package com.zxl.working.utils;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
public class HttpUtil {

    private static Logger logger = LogManager.getLogger(HttpUtil.class);
    // 配置信息
    private RequestConfig requestConfig = RequestConfig.custom()
            // 设置连接超时时间(单位毫秒)
            .setConnectTimeout(5000)
            // 设置请求超时时间(单位毫秒)
            .setConnectionRequestTimeout(5000)
            // socket读写超时时间(单位毫秒)
            .setSocketTimeout(5000)
            // 设置是否允许重定向(默认为true)
            .setRedirectsEnabled(true).build();

    public String get(String url){
        HttpGet httpGet = new HttpGet(url);

        // 将上面的配置信息 运用到这个Get请求里
        httpGet.setConfig(requestConfig);

        return this.exeUrl(httpGet);

    }

    public String post(String url,String paramJson){

        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(paramJson, "UTF-8");

        httpPost.setEntity(entity);

        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        httpPost.setConfig(requestConfig);

        return this.exeUrl(httpPost);
    }

    private String exeUrl(HttpUriRequest request){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;

        try {

            // 由客户端执行(发送)Get请求
            response = httpClient.execute(request);

            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            logger.info("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                String responseStr = EntityUtils.toString(responseEntity);
                logger.info("响应内容长度为:" + responseEntity.getContentLength());
                logger.info("响应内容为:" + responseStr);
                return responseStr;
            }
            return null;
        } catch (ClientProtocolException e) {
            logger.error("ClientProtocolException",e);
            return null;
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("ParseException",e);
            return null;
        } catch (IOException e) {
            logger.error("IOException",e);
            return null;
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                logger.error("IOException",e);
            }
        }
    }
}
